package com.zhiche.lisa.util;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Soap1_1client {

//	/**
//	 * soap1.1 发送报文
//	 * @param postUrl
//	 * @param soapXml
//	 * @param soapAction
//	 * @return
//	 * @throws IOException
//	 */
//	public static String doPostSoap1_1(String postUrl, String soapXml,
//			String soapAction){
//
//		String retStr = "";
//		CloseableHttpResponse response = null;
//
//		// 创建HttpClientBuilder
//		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
//
//		// HttpClient
//		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
//		HttpPost httpPost = new HttpPost(postUrl);
//
//		// 设置请求和传输超时时间
//		RequestConfig requestConfig = RequestConfig.custom()
//				.setSocketTimeout(ConfigConstant.EXCHANGE_SOCKETTIMEOUT)
//				.setConnectTimeout(ConfigConstant.EXCHANGE_CONNECTTIMEOUT)
//				.build();
//
//		httpPost.setConfig(requestConfig);
//
//		try {
//
//			httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
//			httpPost.setHeader("SOAPAction", soapAction);
//			StringEntity data = new StringEntity(soapXml,
//					Charset.forName("UTF-8"));
//			httpPost.setEntity(data);
//			response = closeableHttpClient.execute(httpPost);
//			HttpEntity httpEntity = response.getEntity();
//			if (httpEntity != null) {
//				// 打印响应内容
//				retStr = EntityUtils.toString(httpEntity, "UTF-8");
//			}
//
//			if (null != closeableHttpClient){
//				// 释放资源
//				closeableHttpClient.close();
//			}
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return retStr;
//	}
	
	/**
	 * 转换返回结果为map
	 * 
	 * @param responseList
	 * @return
	 * @throws DocumentException
	 */
	public static Map<String, String> getResponseMap(String responseList)
			throws DocumentException {
		responseList = responseList.replace("&lt;", "<").replace("&gt;", ">");
		responseList = responseList.replaceAll("&", "&amp;");
		Document doc = DocumentHelper.parseText(responseList);

		Map map = Dom2Map(doc);
		return (Map<String, String>) ((Map) map.get("Body"))
				.get("SendResponse");
	}
	
	/**
	 * Document 转map
	 * @param doc
	 * @return
	 */
    public static Map<String, Object> Dom2Map(Document doc){  
        Map<String, Object> map = new HashMap<String, Object>();  
        if(doc == null)  
            return map;  
        Element root = doc.getRootElement(); 
        map= Dom2Map(root); 
        return map;  
    }  
    
    /**
     * Element转map
     * @param e
     * @return
     */
	public static Map Dom2Map(Element e){  
        Map map = new HashMap();  
        List list = e.elements();  
        if(list.size() > 0){  
            for (int i = 0;i < list.size(); i++) {  
                Element iter = (Element) list.get(i);  
                List mapList = new ArrayList();  
                  
                if(iter.elements().size() > 0){  
                    Map m = Dom2Map(iter);  
                    if(map.get(iter.getName()) != null){  
                        Object obj = map.get(iter.getName());  
                        if(!obj.getClass().getName().equals("java.util.ArrayList")){  
                            mapList = new ArrayList();  
                            mapList.add(obj);  
                            mapList.add(m);  
                        }  
                        if(obj.getClass().getName().equals("java.util.ArrayList")){  
                            mapList = (List) obj;  
                            mapList.add(m);  
                        }  
                        map.put(iter.getName(), mapList);  
                    }else  
                        map.put(iter.getName(), m);  
                }  
                else{  
                    if(map.get(iter.getName()) != null){  
                        Object obj = map.get(iter.getName());  
                        if(!obj.getClass().getName().equals("java.util.ArrayList")){  
                            mapList = new ArrayList();  
                            mapList.add(obj);  
                            mapList.add(iter.getText());  
                        }  
                        if(obj.getClass().getName().equals("java.util.ArrayList")){  
                            mapList = (List) obj;  
                            mapList.add(iter.getText());  
                        }  
                        map.put(iter.getName(), mapList);  
                    }else  
                        map.put(iter.getName(), iter.getText());  
                }  
            }  
        }else  
            map.put(e.getName(), e.getText());  
        return map;  
    }  
}
